﻿using FlyingCircusWebIS.Controllers.Utilidades;
using FlyingCircusWebIS.Filtros;
using FlyingCircusWebIS.Models;
using FlyingCircusWebIS.ViewModels.AeronaveVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlyingCircusWebIS.Controllers
{
    [AutenticacaoFilter("A")]
    public class AeronaveController : Controller
    {
        IAeronaveService aeronaveService;

        public AeronaveController()
        {
            aeronaveService = new AeronaveService();
        }

        public ActionResult Index()
        {
            AeronaveIndexVM vm = ConstruirIndexVM();
            return View(vm);
        }

        public ActionResult Form(int? id)
        {
            AeronaveFormVM vm;
            if (id.HasValue)
            {
                vm = ConstruirFormVMParaEdicao(id.Value);
                if (vm == null)
                    return new HttpNotFoundResult();
            }
            else
            {
                vm = ConstruirFormVMParaNovo();
            }

            return View(vm);
        }

        [HttpPost]
        public ActionResult Form(AeronaveFormVM vm)
        {
            if (ModelState.IsValid)
            {
                Aeronave registro = ConverterFormVM(vm);

                var erros = aeronaveService.ValidarEntidade(registro);
                if (erros.Count == 0)
                {
                    if (vm.Edicao)
                        aeronaveService.Atualizar(registro);
                    else
                        aeronaveService.Adicionar(registro);

                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelErrors(erros);
                }
            }

            PopulaItensFormVM(vm);
            return View(vm);
        }


        public JsonResult Remover(int id)
        {
            JsonActionResultModel jarm = new JsonActionResultModel();

            var erros = aeronaveService.ValidaRemoverPorId(id);
            if (erros.Count == 0)
            {
                aeronaveService.RemoverPorId(id);
                jarm.Erro = false;
                jarm.Mensagem = "Registro removido com sucesso!";
            }
            else
            {
                jarm.Erro = true;
                jarm.Mensagem = "Não é possível remover o registro.\n";
                jarm.AddValidationResultErros(erros);
            }

            return Json(jarm);
        }

        public JsonResult Desativar(int id)
        {
            aeronaveService.DesativarPorId(id);
            return Json(new JsonActionResultModel("Registro desativado com sucesso."));
        }

        public JsonResult Ativar(int id)
        {
            aeronaveService.AtivarPorId(id);
            return Json(new JsonActionResultModel("Registro ativado com sucesso."));
        }



        // -----------------------------------------------------------------------------------------
        // Métodos auxiliares 
        // -----------------------------------------------------------------------------------------


        private AeronaveFormVM ConstruirFormVMParaNovo()
        {
            AeronaveFormVM vm = new AeronaveFormVM();
            vm.Edicao = false;
            vm.FlagAtivo = true;
            PopulaItensFormVM(vm);
            return vm;
        }


        private AeronaveFormVM ConstruirFormVMParaEdicao(int id)
        {
            Aeronave registro = aeronaveService.BuscarPorId(id);
            AeronaveFormVM vm = null;
            if (registro != null)
            {
                vm = ConverterFormVM(registro);
                vm.Edicao = true;
                PopulaItensFormVM(vm);
            }
            return vm;
        }

        private AeronaveFormVM ConverterFormVM(Aeronave registro)
        {
            AeronaveFormVM vm = new AeronaveFormVM();
            vm.IdAeronave = registro.IdAeronave;
            vm.Descricao = registro.Descricao;
            vm.FlagAtivo = registro.FlagAtivo.Equals("S");
            vm.IdModeloAeronave = registro.IdModeloAeronave;

            return vm;
        }

        private Aeronave ConverterFormVM(AeronaveFormVM vm)
        {
            Aeronave registro = new Aeronave();
            registro.IdAeronave = vm.IdAeronave;
            registro.Descricao = vm.Descricao;
            registro.FlagAtivo = (vm.FlagAtivo ? "S" : "N");
            registro.IdModeloAeronave = vm.IdModeloAeronave;

            return registro;
        }

        private void PopulaItensFormVM(AeronaveFormVM vm)
        {
            vm.ItensModeloAeronave = aeronaveService.ListarItensModeloAeronave();
        }


        private AeronaveIndexVM ConstruirIndexVM()
        {
            AeronaveIndexVM vm = new AeronaveIndexVM();

            var registros = aeronaveService.Listar();
            vm.Registros = registros;
            vm.TotalRegistros = registros.Count;
            return vm;
        }




    }
}
